<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增机场</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <AirportDrawer @register="registerDrawer" @success="handleSuccess" />
    <AirportGiveSetDrawer @register="registerAirportGiveSetDrawer" />
  </div>
</template>
<script lang="ts" setup name="airport">
import { BasicTable, TableAction, ActionItem } from '@apps/antd-admin/src/components/Table';
import { useDrawer } from '@apps/antd-admin/src/components/drawer';
import { useBaseTable } from '@apps/antd-admin/src/hooks';
import { columns, searchFormSchema } from './data/data';
import { Api } from './data/api';
import AirportDrawer from './components/AirportDrawer.vue';
import AirportGiveSetDrawer from './components/AirportGiveSetDrawer.vue';

const [registerDrawer, { openDrawer }] = useDrawer();
const [registerAirportGiveSetDrawer, { openDrawer: openAirportGiveSetDrawer }] = useDrawer();
const { prefixCls, tableContext, handleDelete, handleSuccess, handlePost, handleBatchDelete } = useBaseTable({
  designScope: 'airport-template',
  tableProps: {
    //表格标题
    title: '机场列表',
    //主键字段
    rowKey: 'airportId',
    //列表接口
    listUrl: Api.page,
    handlerName: 'busAirportListHandler',
    //删除接口
    delUrl: Api.remove,
    //列定义
    columns,
    //查询表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    //操作列配置
    actionColumn: {
      width: 140,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  },
});

const [registerTable, { reload }, { rowSelection, hasSelectedRows }] = tableContext;

/**
 * 行内操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    },
    {
      label: '赠送设置',
      onClick: handleAirportGiveSet.bind(null, record),
    },
  ];
}

function handleCreate() {
  openDrawer(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
  });
  reload();
}
function handleAirportGiveSet(record: Recordable) {
  openAirportGiveSetDrawer(true, {
    record,
    isUpdate: true,
  });
}
</script>
